<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  Filter Acces Rightes
//-----------------------------------------------------------------

if(!isset($is_webmaster))
{ header("location: $PHP_SELF?i=2"); exit; }

switch($a)
{
	//-----------------------------------------------------------------
	//  Install Blocks
	//-----------------------------------------------------------------

	case 1:

		//-----------------------------------------------------------------
		//  Install the block
		//-----------------------------------------------------------------

		$db->insertRow("blocks",array('name' => ''.$add_block.'','used' => '0'));

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/blocks&i=57");

	break;

	//-----------------------------------------------------------------
	//  Uninstall Blocks
	//-----------------------------------------------------------------

	case 2:

		//-----------------------------------------------------------------
		//  Uninstall the block and remove from templates too
		//-----------------------------------------------------------------

		$checker = $db->getLine("SELECT * FROM blocks WHERE id = '$d'");

		$db->deleteRow("blocks","id = '{$checker['id']}'");
		$db->deleteRow("template","name = '{$checker['name']}'");

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/blocks&i=58");

	break;

	//-----------------------------------------------------------------
	//  Show list + form
	//-----------------------------------------------------------------

	default:

		//-----------------------------------------------------------------
		// Echo the back link
		//-----------------------------------------------------------------

		echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='head'><td colspan='3'>{$language['Admin name']}</td></tr><tr><td colspan='3' class='con1' onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='$PHP_SELF?n=modules/admin'><a href='$PHP_SELF?n=modules/admin'> &raquo; {$language['Admin back']}</a></td></tr></table><br>";

		//-----------------------------------------------------------------
		//  Code for Installed Block
		//-----------------------------------------------------------------

		$blocks_db_blocks = $db->getTable("SELECT * FROM blocks");

		if(!isset($blocks_installed)) $blocks_installed = NULL;
		if(!isset($exclude_installed_blocks)) $exclude_installed_blocks = NULL;

		if($blocks_db_blocks != FALSE)
		{
			foreach($blocks_db_blocks as $idx=>$blocks_id)
			{
				//-----------------------------------------------------------------
				//  Make Layout
				//-----------------------------------------------------------------

				$blocks_installed .= "
					<tr class='".row2color($idx)."' onMouseOver=this.className='con3' onMouseOut=this.className='".row2color($idx)."'>
					<td>{$blocks_id['name']}</td>
					<td><a href='$PHP_SELF?n=modules/blocks&a=2&d={$blocks_id['id']}'><img src='templates/$template/images/buttons/delete.gif' border='0' alt='{$language['Blocks view desc2']}'></a></td>
					</tr>";

				$exclude_installed_blocks .= "{$blocks_id['name']}$|";
			}
		}
		else
		{
			//-----------------------------------------------------------------
			//  Make Layout
			//-----------------------------------------------------------------

			$blocks_installed .= "
				<tr class='con1'>
				<td colspan='3'>{$language['Blocks view desc3']}</td>
				</tr>\n";
		}

		//-----------------------------------------------------------------
		//  Load dropdown_form for templates directory
		//-----------------------------------------------------------------

		$array_blocks = array();
		$dir_blocks = opendir( './blocks' );

		while ($file_blocks = readdir($dir_blocks))
			if(!preg_match( "/^..?$|^index|htm$|html$|$exclude_installed_blocks^\./i", $file_blocks))
				$array_blocks[] = array($file_blocks, $file_blocks);

		closedir($dir_blocks);
		sort($array_blocks);
		reset($array_blocks) 
		
		?>
			<form method="post" action="<?= $PHP_SELF ?>?n=modules/blocks">
			<table class="main" cellspacing="1" cellpadding="4" width="100%">
			<? if($array_blocks != NULL){ ?><col width="80%"><col width="20%"><? } ?>
			<tr class="head"><td colspan="3"><?= $language['Blocks add title'] ?></td></tr>
			<tr class="bottom"><td><?= $language['Blocks add desc'] ?></td><? if($array_blocks != NULL){ ?><td><?= $language['Blocks add desc2'] ?></td><? } ?></tr>
			<tr>
			<td class="con1"><? if($array_blocks != NULL) echo form_dropdown('add_block', $array_blocks, $config['template'], "style='width:100%'", "class='box'", "tabindex='8'"); else echo $language['Blocks add desc4'] ?></td><? if($array_blocks != NULL) { ?><td class="con1"><input type="submit" name="submit" style="width:100%" class="box" value="<?= $language['Blocks add desc3'] ?>" tabindex="3"></td><? } ?></tr>
			</tr>
			<input type="hidden" name="a" value="1">
			</table></form>
			<br>
			<table class="main" cellspacing="1" cellpadding="4" width="100%">
			<col width="94%"><col width="5%">
			<tr class="head"><td colspan="3"><?= $language['Blocks view title'] ?></td></tr>
			<tr class="bottom"><td colspan="2"><?= $language['Blocks view desc'] ?></td></tr>
			<?= $blocks_installed ?>
			</table>
		<?
}
?>